﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json;
using System.IO;
using System.Net.Http.Formatting;

namespace PivotData.Tests
{
    [TestClass]
    public class FacetStringTest
    {
        [TestMethod]
        public void TestEqualString()
        {
            var f1 = new FacetString() { Name = "1", Value = "2" };
            var f2 = new FacetString() { Name = "1", Value = "2" };

            Assert.IsTrue(f1.Equals(f2), "equal");
        }

        [TestMethod]
        public void TestNEqualString()
        {
            var f1 = new FacetString() { Name = "1", Value = "2" };
            var f2 = new FacetString() { Name = "2", Value = "2" };

            Assert.IsFalse(f1.Equals(f2), "not equal");
        }

        [TestMethod]
        public void TestNEqualStringV()
        {
            var f1 = new FacetString() { Name = "1", Value = "2" };
            var f2 = new FacetString() { Name = "1", Value = "1" };

            Assert.IsFalse(f1.Equals(f2), "not equal");
        }

        [TestMethod]
        public void TestNEqualStringOtherObj()
        {
            var f1 = new FacetString() { Name = "1", Value = "2" };
            var f2 = new FacetNumber() { Name = "1", Number = 10 };

            Assert.IsFalse(f1.Equals(f2), "not equal");
        }

        [TestMethod]
        public void TestJsonSimpleStringConversion()
        {
            var s = "[{\"Name\": \"dude\", \"Value\": \"freak\"}]";
            var jmf = new JsonMediaTypeFormatter();
            var o = JsonConvert.DeserializeObject<FacetAbsBase[]>(s, jmf.SerializerSettings);

            Assert.AreEqual(1, o.Length, "# of facets");
            Assert.IsInstanceOfType(o[0], typeof(FacetString), "Facet type");
            Assert.AreEqual("dude", (o[0] as FacetString).Name, "Name of facet");
            Assert.AreEqual("freak", (o[0] as FacetString).Value, "Value of facet");
        }

        [TestMethod]
        public void TestJsonWithTypeStringConversion()
        {
            var s = "[{\"__type\": \"PivotWSCollection.FacetString:#PushImages\", \"Name\": \"dude\", \"Value\": \"freak\"}]";

            // We need to use the json converter seralizer settings.
            var jmf = new JsonMediaTypeFormatter();
            var o = JsonConvert.DeserializeObject<FacetAbsBase[]>(s, jmf.SerializerSettings);


            Assert.AreEqual(1, o.Length, "# of facets");
            Assert.IsInstanceOfType(o[0], typeof(FacetString), "Facet type");
            Assert.AreEqual("dude", (o[0] as FacetString).Name, "Name of facet");
            Assert.AreEqual("freak", (o[0] as FacetString).Value, "Value of facet");
        }

        [TestMethod]
        public void TestJsonRoundTripStringConversionJson()
        {
            var fsOrig = new FacetString() { Name = "hi", Value = "there" };
            var formatter = new JsonMediaTypeFormatter();
            var stream = new MemoryStream();

            formatter.WriteToStreamAsync(typeof(FacetAbsBase[]), new FacetAbsBase[] { fsOrig }, stream, content: null, transportContext: null).Wait();
            stream.Position = 0;
            var roundtrippedError = formatter.ReadFromStreamAsync(typeof(FacetAbsBase[]), stream, content: null, formatterLogger: null).Result as FacetAbsBase[];

            Assert.IsNotNull(roundtrippedError);
            Assert.AreEqual(1, roundtrippedError.Length);
            Assert.IsInstanceOfType(roundtrippedError[0], typeof(FacetString));
            Assert.AreEqual("hi", roundtrippedError[0].Name, "name of fs string");
            Assert.AreEqual("there", (roundtrippedError[0] as FacetString).Value, "value of fs string");
        }
    }
}
